//
//  ViewController.swift
//  ZLPullMenuSwift
//
//  Created by zzl on 01/16/2025.
//  Copyright (c) 2025 zzl. All rights reserved.
//

import UIKit
import SnapKit
import ZLPullMenuSwift

class ViewController: UIViewController, ZLPullMenuDelegate {
    // MARK: - Properties
    
    /// 菜单
    private lazy var menu: ZLPullMenu = {
        let menu = ZLPullMenu()
        menu.layoutMode = .center
        menu.delegate = self
        menu.menus = menus
        return menu
    }()
    
    /// 菜单内容
    private lazy var menus: [ZLPullMenuItem] = {
        // 顶部菜单样式
        let menuConfig = ZLPullMenuConfig.config(
            font: .systemFont(ofSize: 14),
            selectedFont: .systemFont(ofSize: 14, weight: .bold),
            textColor: UIColor(red: 0.39, green: 0.39, blue: 0.39, alpha: 1.00),
            textSelectedColor: UIColor(red: 0.79, green: 0.55, blue: 0.30, alpha: 1.00),
            arrowColor: UIColor(red: 0.39, green: 0.39, blue: 0.39, alpha: 1.00),
            arrowSelectedColor: UIColor(red: 0.79, green: 0.55, blue: 0.30, alpha: 1.00)
        )
        
        // 组标题配置
        let groupConfig = ZLPullMenuGroupConfig.config(
            font: .systemFont(ofSize: 16, weight: .bold),
            textColor: .orange
        )
        
        // 选项样式
        let optionConfig = ZLPullMenuOptionConfig.config(
            font: .systemFont(ofSize: 12),
            selectedFont: .systemFont(ofSize: 12, weight: .bold),
            textColor: UIColor(red: 0.31, green: 0.31, blue: 0.31, alpha: 1.00),
            textSelectedColor: .black,
            backgroundColor: UIColor(red: 0.96, green: 0.96, blue: 0.96, alpha: 1.00),
            backgroundSelectedColor: UIColor(red: 0.98, green: 0.87, blue: 0.28, alpha: 1.00),
            borderColor: .clear,
            borderSelectedColor: UIColor(red: 0.96, green: 0.96, blue: 0.96, alpha: 1.00)
        )
        
        // 以下数据配置
        // 综合排序
        let group11 = ZLPullMenuGroup.group(
            title: "默认标题样式",
            selecteds: true,
            mustSelect: false,
            options: [
                .option(title: "专送", selected: false),
                .option(title: "自取", selected: false)
            ]
        )
        
        let group12 = ZLPullMenuGroup.group(
            title: "自定义标题样式",
            selecteds: true,
            mustSelect: false,
            options: [
                .option(title: "默认样式", selected: false),
                .option(title: "自定义样式", selected: false, config: optionConfig),
                .option(title: "默认选中", selected: true),
                .option(title: "60分钟", selected: false),
                .option(title: "70分钟", selected: false),
                .option(title: "80分钟", selected: false)
            ],
            config: groupConfig
        )
        
        let group13 = ZLPullMenuGroup.group(
            title: "子选项多选",
            options: [
                .option(title: "预定", selected: false),
                .option(title: "准时", selected: false),
                .option(title: "放心吃", selected: false)
            ]
        )
        
        let group14 = ZLPullMenuGroup.group(
            title: "子选项单选",
            selecteds: false,
            mustSelect: false,
            options: [
                .option(title: "商家优惠", selected: false),
                .option(title: "随机满减", selected: false),
                .option(title: "商品折扣", selected: false),
                .option(title: "随机返现", selected: false),
                .option(title: "新店立减", selected: false)
            ]
        )
        
        let item1 = ZLPullMenuItem.item(
            title: "自定义样式",
            groups: [group11, group12, group13, group14],
            config: menuConfig
        )
        
        // 尺寸
        let group21 = ZLPullMenuGroup.group(
            title: "选项",
            selecteds: false,
            mustSelect: false,
            options: [
                .option(title: "36码", selected: false),
                .option(title: "37码", selected: false),
                .option(title: "38码", selected: false),
                .option(title: "39码", selected: false)
            ]
        )
        
        let item2 = ZLPullMenuItem.item(
            title: "默认样式",
            groups: [group21]
        )
        
        // 颜色
        let group31 = ZLPullMenuGroup.group(
            title: "选项",
            options: [
                .option(title: "黄色", selected: false),
                .option(title: "白色", selected: false),
                .option(title: "蓝色", selected: false)
            ]
        )
        
        let item3 = ZLPullMenuItem.item(
            title: "默认样式",
            groups: [group31]
        )
        
        return [item1, item2, item3]
    }()
    
    // MARK: - Lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(menu)
        menu.snp.makeConstraints { make in
            make.top.equalTo(68)
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
        }
    }
    
    // MARK: - ZLPullMenuDelegate
    
    func pullMenuDidSelected(_ indexPath: ZLIndexPath) {
        print("选中下标：\(indexPath.menuIndex) \(indexPath.groupIndex) \(indexPath.optionIndex)")
    }
    
    func pullMenuWillSelected(_ indexPath: ZLIndexPath) -> Bool {
        return true
    }
}

